package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Role;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.RoleService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;


    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Map<String,Object> map = roleService.findById(id);
            return new Result(true, "查询角色列表成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色列表失败");
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody Role role){
        try {
            roleService.add(role);
            return new Result(true,"添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
    }
    @RequestMapping("findByPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("updatePermission")
    public Result updatePermission(Integer id,@RequestBody  Integer[] ids){
        try {
            roleService.updatePermission(id,ids);
            return new Result(true,"修改权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改权限失败");
        }
    }

}
